f = open("D:/STUDY/python/文件/word.txt", "r", encoding="UTF-8")
# 方法一：运用count函数计算字符串出现次数
content=f.read()
count=content.count("learning")
print(f"learning在文本文件中出现了：{count}次")


with open("D:/STUDY/python/文件/word.txt", "r", encoding="UTF-8") as f:
    count = 0
    for line in f:
        # line.replace("\n"," ")#用 替换\n
        line = line.strip()  # 去除开头和结尾的空格和\n
        words = line.split(" ")
        for word in words:
            if word == "learning":
                count += 1

print(f"出现的次数为{count}")
